<?php
require_once 'pages/TMSFrontListPage.inc';

class TMSFrontCategoryPage extends TMSFrontListPage {
/**
 * @var TCandyItem
 */	
	protected $categoryItem;
	
	private $_key_words_;
	private $_description_;
	protected $_bool_strict_;

	protected $_int_sub_page_size_ = 0;

	protected $scTotals = array();
	
	public function Handle(THttpRequest $request){
		if (isset($request->id))
			$this->categoryItem = $this->CategoriesModel->GetItem($this->getCategoryId($request->id));
		if ($this->categoryItem)
			return parent::Handle($request);
		$this->service->RedirectToNotFound();	
	}
	
	private function _fill_categories(TCandyItem $category,array &$categories){
		$categories[] = $category->Base->Id;
		foreach ($category->Contents() as $c)
			$this->_fill_categories($c, $categories);
	}
	
	protected function getFetchOptions(){
		$lo = parent::getFetchOptions();
		$categories = array();
		if ($this->categoryItem){
			$categories[] = $this->categoryItem->Base->Id;
			if (!$this->Strict){
				$subs = $this->CategoriesModel->GetContents($this->categoryItem->ItemId(),false);
				foreach ($subs as $c) $this->_fill_categories($c, $categories);
			}		
			$lo->Filter = new TFilterOption('Categories', TConditionType::C_IN, $categories);
		}
		$lo->Filter = new TFilterOption('IsPublished', TConditionType::C_EQUAL, true);
		$lo->Filter = new TFilterOption('Language',TConditionType::C_EQUAL, $this->Application()->Language);
		
		$lo->Sorting = new TSortingOption('Published',TSortingType::SORT_DESC);
		return $lo;
	}
	
	protected function processUrlParameters(array $parameters){
		if (!isset($parameters['id']))
			if ($this->categoryItem)
				$parameters['id'] = $this->categoryItem->ItemId();
		return $parameters;
	}	
	
	public function Pages(){
		return new TMSFrontPageItemIterator($this->PagesModel->GetList($this->getFetchOptions(),$this->totalPageCount), $this);
	}
	
	public function SubItems(){
		if ($this->categoryItem){
			$pi = new TMSFrontPageItem($this->categoryItem, $this);
			return $pi->Subs();
		}
		return new TArrayIterator(array());		
	}
	
	public function SubCategories(){
		if ($this->categoryItem)
			return new TMSFrontPageItemIterator($this->CategoriesModel->GetContents($this->categoryItem->ItemId(),true,new TListOptions(array(new TFilterOption('IsPublished', TConditionType::C_EQUAL, true),new TFilterOption('Language', TConditionType::C_EQUAL, $this->Application()->Language)),array(new TSortingOption('OrderNumber')))),$this);
		return new TArrayIterator(array());	
	}
	
	public function SubCategoryPages($category){
		$lo = TMSFrontListPage::getFetchOptions();
		if ($this->SubPageSize)
			$lo->Count = $this->SubPageSize;
		$categories = array();
		if ($category){
			$subs = $this->CategoriesModel->GetContents($category,false);
			$categories[] = $category;
			foreach ($subs as $c) $this->_fill_categories($c, $categories);
			$lo->Filter = new TFilterOption('Categories', TConditionType::C_IN, $categories);
		}
		$lo->Filter = new TFilterOption('IsPublished', TConditionType::C_EQUAL, true);
		$lo->Filter = new TFilterOption('Language', TConditionType::C_EQUAL, $this->Application()->Language);
		$this->scTotals[$category] = 0;
		return new TMSFrontPageItemIterator($this->PagesModel->GetList($lo,$this->scTotals[$category]), $this);
	}	
	
	public function SubCategoryPagesTotal($category){
		return isset($this->scTotals[$category])?$this->scTotals[$category]:0;
	}
	
	public function IsParentItem(TMSFrontPageItem $item){
		if (parent::IsParentItem($item))
			return true;
		if ($this->categoryItem)
			return $item->ItemId() == $this->categoryItem->ItemId() && $item->ClassName() == $this->categoryItem->ClassName();
		return false;
	}
		
	public function Path(){
		$result = array();
		if ($this->categoryItem){
			$p = $this->categoryItem->Container();
			while ($p && ($p->Slug != $this->service->MainMenuRoot)){
				if ($p->Language == $this->Application()->Language)
					$result[] = new TMSFrontPageItem($p,$this);
				$p = $p->Container();
			}
		}
		return array_reverse($result);
	}
	
	public function ContentsCategory(){
		return $this->categoryItem;
	}
	
	private function _load_meta(){
		$pages = $this->PagesModel->GetList($this->getFetchOptions(),$this->totalPageCount);
		$res = array();
		foreach ($pages as $page)
			$res[] = $page->Title;
		$this->_key_words_ = join(',', $res);
		$this->_description_ = $this->_key_words_;
	}
	
	public function Keywords(){
		if (!$this->_key_words_)
			$this->_load_meta();
		return $this->_key_words_;
	}
	
	public function Description(){
		if (!$this->_description_)
			$this->_load_meta();
		return $this->_description_;
	}
	
	public function Title(){
		if ($this->categoryItem)
			return $this->categoryItem->Name;
		return parent::Title();	
	}
}